//
//  LiveNewSignViewSec.m
//  LottieAnimation
//
//  Created by tianlong on 2020/9/28.
//  Copyright © 2020 tianlong. All rights reserved.
//

#import "LiveNewSignViewSec.h"
#import "Masonry.h"

@interface LiveNewSignViewSec ()
@property (weak,   nonatomic) UIImageView *headBgView;

@end

@implementation LiveNewSignViewSec
- (instancetype)initWithStuName:(NSString *)stuName
                      stuAvatar:(NSString *)stuAvatar {
    self = [super initWithStuName:stuName stuAvatar:stuAvatar];
    if (self) {
        
        UIImageView *headBgView = [[UIImageView alloc] init];
        headBgView.image = [UIImage imageNamed:@"livenew_myclass_yinying"];
        [self addSubview:headBgView];
        self.headBgView = headBgView;
        
        UIImageView *headView = [[UIImageView alloc] init];
        headView.backgroundColor = [UIColor blueColor];
//        [headView sd_setImageWithURL:[NSURL URLWithString:stuAvatar] placeholderImage:[UIImage imageNamed:LiveNewMyClassStuPlaceHolderImg]];
        headView.backgroundColor = [UIColor blueColor];
        [self addSubview:headView];
        self.headView = headView;

        UIColor *textColor = [UIColor colorWithHexValue:0xD16B28];
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text = [NSString stringWithFormat:@"%@童鞋你好~",self.stuName];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.textColor = textColor;
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        UILabel *descLabel = [[UILabel alloc] init];
        descLabel.text = @"点击签到开启美好的一堂课哦!";
        descLabel.textColor = textColor;
        descLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:descLabel];
        self.descLabel = descLabel;
        
        UILabel *tipLabel = [[UILabel alloc] init];
        tipLabel.hidden = YES;
        tipLabel.text = @"签到成功~";
        tipLabel.textColor = textColor;
        tipLabel.numberOfLines = 2;
        tipLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:tipLabel];
        self.tipLabel = tipLabel;
        
        UIFont *btnFont = [UIFont fontPingFangSCMediumWithSize:15 *self.bigClassScale];
        UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [signBtn.titleLabel setFont:btnFont];
        [signBtn setTitleColor:[UIColor colorWithHexValue:0xFEFEFE] forState:UIControlStateNormal];
        [signBtn setTitle:@"签到" forState:UIControlStateNormal];
        [signBtn addTarget:self action:@selector(clickedSignBtn:) forControlEvents:UIControlEventTouchUpInside];
        signBtn.backgroundColor = [UIColor colorWithHexValue:0xE02727];
        signBtn.backgroundColor = [UIColor redColor];
        [self addSubview:signBtn];
        self.signBtn = signBtn;
        
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [closeBtn.titleLabel setFont:btnFont];
        [closeBtn addTarget:self action:@selector(clickedCloseBtn:) forControlEvents:UIControlEventTouchUpInside];
        [closeBtn setBackgroundImage:[UIImage imageNamed:@"livenew_mycalss_close"] forState:UIControlStateNormal];
        [self addSubview:closeBtn];
        self.closeBtn = closeBtn;
    }
    return self;
}

- (void)setupLayout {
    
    __weak typeof(self)selfWeak = self;
    
    CGFloat horScale = self.width/(246.0f);//水平缩放系数
    CGFloat verScale = self.height/(177.0f-(50-19));//垂直缩放系数177:面板高度，50:头像高度，19:头像顶部距离面板的高度
    
    CGFloat headW = 60 *horScale;
    CGFloat top = 19 *verScale;
    CGFloat borderWidth = 4;
    self.headView.layer.cornerRadius = headW *0.5;
    self.headView.layer.masksToBounds = YES;
    self.headView.layer.borderColor = [UIColor colorWithHexValue:0xFFFBE5].CGColor;
    self.headView.layer.borderWidth = borderWidth;
    [self.headView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(headW);
        make.centerX.equalTo(selfWeak);
        make.top.offset(-top);
    }];
    
    CGFloat headbgW = (headW + borderWidth);
    [self.headBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(headbgW);
        make.centerY.equalTo(selfWeak.headView.mas_centerY).offset(borderWidth+2);
        make.centerX.equalTo(selfWeak.headView);
    }];

    CGFloat nameTop = 46 *verScale;
    CGFloat nameW = (self.width-20) *horScale;
    CGFloat nameH = 12.5 *verScale;
    self.nameLabel.font = [UIFont fontPingFangSCMediumWithSize:13 *horScale];
    [self.nameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selfWeak).offset(nameTop);
        make.centerX.equalTo(selfWeak);
        make.width.offset(nameW);
        make.height.offset(nameH);
    }];
    
    CGFloat descTop = 70 *verScale;
    CGFloat descW = (self.width-20) *horScale;
    CGFloat descH = 12 *verScale;
    self.descLabel.font = [UIFont fontPingFangSCMediumWithSize:11 *horScale];
    [self.descLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selfWeak).offset(descTop);
        make.centerX.equalTo(selfWeak);
        make.width.offset(descW);
        make.height.offset(descH);
    }];
    
    CGFloat tipTop = 92 *verScale;
    CGFloat tipLeft = 34 *verScale;
    self.tipLabel.font = [UIFont fontPingFangSCMediumWithSize:11 *horScale];
    [self.tipLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selfWeak).offset(tipTop);
        make.left.equalTo(selfWeak).offset(tipLeft);
        make.right.equalTo(selfWeak).offset(-tipLeft);
        make.centerX.equalTo(selfWeak);
    }];
    
    CGFloat btnW = 90 *horScale;
    CGFloat btnH = 32 *verScale;
    CGFloat btnTop = 98 *verScale;
    UIFont *btnFont = [UIFont fontPingFangSCMediumWithSize:15 *horScale];
    [self.signBtn.titleLabel setFont:btnFont];
    self.signBtn.layer.cornerRadius = btnH *0.5;
    self.signBtn.layer.masksToBounds = YES;
    [self.signBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(selfWeak).offset(btnTop);
        make.centerX.equalTo(selfWeak);
        make.width.offset(btnW);
        make.height.offset(btnH);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.width>0&&self.height>0) {
        [self setupLayout];
    }
}

#pragma mark - event
- (void)clickedSignBtn:(UIButton *)button {
    if (self.clickedSignButtonBlock) {
        self.clickedSignButtonBlock();
    }
}

- (void)clickedCloseBtn:(UIButton *)button {
    if (self.clickedCloseButtonBlock) {
        self.clickedCloseButtonBlock();
    }
}

- (UIButton *)getSignButton {
    return self.signBtn;
}


@end
